/*! ******************************************************************************
 *
 * Created on 2021年9月25日
 *
 * Copyright(c) YuZhou Big Data Laboratory CO.,Ltd, 2022.  All rights reserved.
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/

package org.pentaho.di.ui.spoon.navigation.design.classification.category;

import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.LaniusCategoryTreeItem;
import org.yzbdl.lanius.core.plugins.metastore.LdrCategoryEntity;
import org.pentaho.di.ui.core.gui.GUIResource;
import org.pentaho.di.ui.spoon.navigation.Bundle;
import org.pentaho.di.ui.spoon.navigation.design.classification.Classification;
import org.pentaho.di.ui.spoon.navigation.design.classification.plugin.Plugin;

/**
 * 分组, 一般是树的最上级节点，例如输入、输出、应用等
 *
 * @author hs@yzbdl.ac.cn
 */
public class Category extends Bundle {

//	// 每页显示记录数
//	public static final Integer pageRecords = 10;

	/**
	 * data
	 */
	private LdrCategoryEntity ldrCategoryEntity;

	private Set<Plugin> plugins = new LinkedHashSet<>();

	private Map<Integer, Plugin> pluginsMap = new LinkedHashMap<>();

//	// 当前页码
//	private Integer currentPage;
//	// 总页码
//	private Integer totalPage;
//	// 是否分页
//	private Boolean isPagable;

	/**
	 * view
	 */
	private Bundle parent;

	private LaniusCategoryTreeItem treeItem;

	public Category(Bundle parent, LdrCategoryEntity ldrCategoryEntity) {
		super(parent.getSpoon());
		this.parent = parent;
		this.ldrCategoryEntity = ldrCategoryEntity;

		this.treeItem = new LaniusCategoryTreeItem(((Classification) parent).getTree(), SWT.NONE);
		this.treeItem.setText(ldrCategoryEntity.getName());
		this.treeItem.setImage(GUIResource.getInstance().getImageFolder());
		this.treeItem.setExpanded(isExpanded(ldrCategoryEntity.getExpanded()));
		this.treeItem.setCategoryEntity(ldrCategoryEntity);
	}

	public boolean isEmpty() {
		return this.plugins.isEmpty();
	}

	public void dispose() {
		this.treeItem.dispose();
	}

	public void setPlugins(Set<Plugin> plugins) {
		this.plugins = plugins;
		for (Plugin plugin : plugins) {
			this.pluginsMap.put(plugin.getId(), plugin);
		}
	}

	public LdrCategoryEntity getCategoryEntity() {
		return ldrCategoryEntity;
	}

	public Bundle getParent() {
		return parent;
	}

	public void setParent(Bundle parent) {
		this.parent = parent;
	}

	public LaniusCategoryTreeItem getTreeItem() {
		return treeItem;
	}

	public void setTreeItem(LaniusCategoryTreeItem treeItem) {
		this.treeItem = treeItem;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (o == null || getClass() != o.getClass()) {
			return false;
		}
		Category category = (Category) o;
		return Objects.equals(ldrCategoryEntity, category.ldrCategoryEntity);
	}

	@Override
	public int hashCode() {
		return Objects.hash(ldrCategoryEntity);
	}
}
